var app = getApp();

Page(Object.assign({},{
    data:{
        options : null,
        inited : true,
        print : {},
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onLoad:function(options){
        var self = this;
        self.setData({
            options : options,
        });
        var pid = options.pid ? options.pid : 0;
        app.com.auth(function(user){
            app.com.http('admin','POST',{op:'addportstart',pid:pid},0,false,'','',function(res){
                if (!res.data.errno) {
                    self.setData({
                        print : res.data.data.print ? res.data.data.print : {type:'0',status:'0',isauto:'0'},
                        inited : true,
                    });
                    // 关闭下拉刷新
                    wx.stopPullDownRefresh();
                }else{
                    app.com.alert( res.data.message,function(){
                        app.globalData.adminurl = app.com.getUrlArgs();
                        if( res.data.errno == 2 ){
                            wx.navigateTo({
                                url : res.data.data.url
                            });
                        }
                    });
                }
            });
        });
        wx.setNavigationBarTitle({
          title: '添加云喇叭',
        });
    },
    onChange(e){
        var self = this;
        var type = e.currentTarget.dataset.type;
        var value = e.detail;
        self.setData({
            ['print.'+type] : value,
        })
    },
    deleterule(e){
        var self = this;
        var id = e.currentTarget.dataset.id;
        for (let i = 0,il = self.data.print.rule; i < il.length; i++) {
            if( il[i].id == id ){
                self.data.print.rule.splice(i,1);
            }
        }
        var isrule = 1;
        if( self.data.print.rule.length <= 0 ) var isrule = 0;

        self.setData({
            'print.isrule' : isrule,
            'print.rule' : self.data.print.rule,
        })
    },
    pub(){
        var self = this;
        var print = self.data.print;

        if( !print.name ) {
            app.com.toast('请填入备注名称');return false;
        }
        self.savedata();
    },
    savedata(){
        var self = this;

        self.data.print.porttoken = self.data.print.token;

        var form = JSON.stringify( self.data.print );
        app.com.http('admin','POST',{op:'pubport',form:form},0,true,'','',function(res){
            if (!res.data.errno) {
                app.com.toast(res.data.message,'success',function(){
                    if( !self.data.print.id ){
                        self.setData({
                            print : {},
                        });
                    }
                });
            }else{
                app.com.toast( res.data.message );
                if( res.data.errno == 2 ){
                    wx.navigateTo({
                        url : res.data.data.url
                    });
                }
            }
        });
    },
    toaddress(){
        wx.chooseAddress({
            success: function (res) {}
        })
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    
}))